# -*- coding:utf-8 -*-
from loguru import logger

from constant.request_contants import REQUEST_POST, REQUEST_GET
from engine.handler.common_handler import ToJsonHandler
from engine.handler.lianjia_handler import LianjiaHandler
from engine.parse_engine import ParseEngine
from engine.save_engine import SaveEngine, MongoSaveEngine
from target.target_api import TargetAPI

if __name__ == '__main__':
    # GET https://wh.lianjia.com/fangjia/priceTrend/

    url = "https://wh.lianjia.com/fangjia/priceTrend/"
    parse_engine = ParseEngine(target_api=TargetAPI(url=url, method=REQUEST_GET))
    req_data = "region=city&region_id=420100"
    parse_data = parse_engine.parse(req_data)
    logger.info(parse_data)

    handle_data = ToJsonHandler().handle(parse_data)
    handle_data = LianjiaHandler().handle(handle_data)
    for d in handle_data:
        d["source"]= "lianjia"
        d["url"]= url+"?"+req_data
        d["city"]= LianjiaHandler.get_city_code(url.split("//")[1][:2])
        d["province"]= LianjiaHandler.get_province_code()

    MongoSaveEngine("c_estate").batch_save(handle_data)
